import random

from django.db import models

from neomodel import (
    StringProperty,
    Relationship,
    StructuredRel,
    FloatProperty,
    StructuredNode,
    IntegerProperty,
    BooleanProperty,
    DateTimeProperty
)


class ContactRel(StructuredRel):
    location_id = StringProperty(required=True, help_text='位置id')  # 关系不能建立索引
    shaftWidth = FloatProperty(required=True, help_text='接触深度1~10', db_property='shaftWidth')
    time = DateTimeProperty(default_now=True, help_text='接触时间')  # 这个在数据库中是以时间戳的形式展示

    @property
    def serialize(self):
        return {
            'location_id': self.location_id,
            'value': self.shaftWidth,
            'time': self.time,
            'from': self.start_node().node_id,
            'to': self.end_node().node_id,
        }

    def update(self, location_id=None, degree=None, time=None):
        """
        :param location_id:
        :param degree:
        :param time:
        :return:
        """
        if location_id:
            self.location_id = location_id
        if degree:
            self.degree = degree
        self.save()


# DROP CONSTRAINT ON (test:Person) ASSERT test.node_id IS UNIQUE 删除唯一性约束
#  测试数据 data = {"name": "123", "permanent_residence": "重庆", "mobile": "电话号码"}

class Person(StructuredNode):
    STATUS = (
        ('1', '健康'),
        ('2', '疑似'),  # 疑似
        ('3', '感染'),  # 感染
    )

    GENDER = (
        ('0', '女'),
        ('1', '男'),  # 疑似
        ('2', '未知'),  # 感染
    )

    IMG_URL = 'http://121.196.107.223:8000/img/'
    card_id = StringProperty(max_length=30, unique_index=True, help_text='卡号')
    node_id = IntegerProperty(help_text='节点id',
                              unique_index=True)  # 这个是通过mysql创建的， 当这里建立成功之后， 如果要给号码再建立索引，他就会报错， 我们需要把号码放到node_id前面

    name = StringProperty(max_length=10, required=True, help_text='姓名')
    permanent_residence = StringProperty(max_length=30, required=True, help_text='常驻地')
    mobile = StringProperty(max_length=11, required=True, unique_index=True, help_text='电话号码')  # 电话号码唯一的
    gender = StringProperty(help_text='性别', choices=GENDER, default='2')
    age = IntegerProperty(help_text='年龄', required=True)
    is_vaccine = BooleanProperty(help_text='是否接种疫苗', required=True)
    # 这个是有默认
    status = StringProperty(help_text='状态', choices=STATUS, default='1')  # 有choices就不能有 max_length
    image = StringProperty(help_text='头像', max_length=110)  # 这个就算数据库没有，我序列化的时候也会出来，

    contact = Relationship('Person', 'CONTACT', model=ContactRel)

    @property
    def serialize(self):
        return {
            'id': self.node_id,
            'permanent_residence': self.permanent_residence,
            'label': self.name,
            'mobile': self.mobile,
            'status': self.get_status_display(),
            'card_id': self.card_id,
            'image': self.image,
            'shape': 'circularImage',
            'gender': self.get_gender_display(),
            'age': self.age,
            'is_vaccine': self.is_vaccine
        }

    def update(self, node_id=None, card_id=None, name=None, permanent_residence=None, mobile=None, status=None,
               image=None, gender=None, age=None, is_vaccine=None):
        if node_id:
            self.node_id = node_id
        if card_id:
            self.card_id = card_id
        if name:
            self.name = name
        if permanent_residence:
            self.permanent_residence = permanent_residence
        if mobile:
            self.mobile = mobile
        if status:
            self.status = status
        if image:
            self.image = image
        if is_vaccine:
            self.is_vaccine = is_vaccine
        if age:
            self.age = age
        if gender:
            self.gender = gender

        self.save()
